package jmars.mobile.misc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class LiteFilter {

	private static int skiprows=5;
	private static String module="yuzhong";
	private static String filename="EntropyTrafficTotalHourly_LAC_CI.txt";
	
	private static String out="E:\\BaiduYunDownload\\afterprocess\\"+module+"\\lite_"+filename;
	private static String in="E:\\BaiduYunDownload\\afterprocess\\"+module+"\\"+filename;
	private static String lookup="E:\\BaiduYunDownload\\afterprocess\\"+module+"\\lac_ci_geolocation.txt";
	
	private static Set<String> lookupSet = new HashSet<String>();
	
	public static void main(String[] args) throws IOException{
		String row;
		String[] cols;
		FileWriter resultWriter;
		resultWriter = new FileWriter(out);
		File flookup = new File(lookup);
		FileInputStream fisLookup = new FileInputStream(flookup);
		BufferedReader readerLookup = new BufferedReader(new InputStreamReader(fisLookup));
		while((row=readerLookup.readLine())!=null){
			if(row.indexOf("lac_hash")!=-1)continue;
			if(row!=null){
				cols=row.split(",");
				if(Float.parseFloat(cols[3])>0)
					lookupSet.add(cols[0]);
			}
		}
		System.out.println("lookup set: "+lookupSet.size());
		File fin = new File(in);
		FileInputStream fis = new FileInputStream(fin);
		BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
		int headercount=0,recordCount=0;
		while((row=reader.readLine())!=null){
			if(headercount<skiprows){
				resultWriter.write(row+"\r\n");
				headercount++;
				continue;
			}
			cols=row.split(",");
			if(lookupSet.contains(cols[0])){
				recordCount++;
				resultWriter.write(row+"\r\n");
			}
		}
		fis.close();
		fisLookup.close();
		resultWriter.close();
		System.out.println("finished."+recordCount);
	}
	

}
